package com.example.demo.api;

import com.example.demo.model.Paging;
import com.example.demo.model.Product;
import com.example.demo.model.Result;
import com.example.demo.param.BasePageParam;
import com.example.demo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api/product")
public class ProductAPI {

    @Autowired
    private ProductService productService;

    /**
     * 分页查询商品
     * @param param 分页查询页码和数量封装对象
     * @return
     */
    @GetMapping("/pageQuery")
    @ResponseBody
    public Result<Paging<Product>> pageQueryProduct(BasePageParam param){
        Result<Paging<Product>> result = new Result<>();
        result.setData(productService.pageQueryProduct(param));
        if (result.getData()!= null) {
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 根据主键查询商品
     * @param id 商品主键
     * @return
     */
    @GetMapping("/get")
    @ResponseBody
    public Result<Product> getProductById(@RequestParam("id") String id){
        Result<Product> result = new Result<>();
        result.setData(productService.get(id));
        if (result.getData() != null) {
            result.setSuccess(true);
        }
        return result;
    }
}
